package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.PeachblossomApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.Peachblossom;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PeachblossomVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @auther : 白月尘
 * @date : 8:48  2021/11/1
 */
@Service
public class PeachblossomService {
    @Autowired
    private FdfsWebServer webServer;

    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private OssTemplate ossTemplate;

    @DubboReference
    private PeachblossomApi peachblossomApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    //接收传音
    public PeachblossomVo recivePeachblossom() {
        //redis的key
        String key = Constants.PEACH_BLOSSOM_KEY + UserHolder.getUserId();
        String s = redisTemplate.opsForValue().get(key);
        if (s == null) {
            redisTemplate.opsForValue().set(key, 4 + "");
            redisTemplate.opsForValue().append("deleteKey", UserHolder.getUserId() + ",");
        } else if (Integer.valueOf(s) != 0) {
            redisTemplate.opsForValue().set(key, Integer.valueOf(s) - 1 + "");
        } else {
            // PeachblossomVo peachblossom = new PeachblossomVo();
            // peachblossom.setRemainingTimes(0);
            // return peachblossom;
            throw new BusinessException(ErrorResult.countZeroError());
        }
        Peachblossom peachblossom = peachblossomApi.recivePeachblossom(UserHolder.getUserId());
        UserInfo userInfo = userInfoApi.findById(peachblossom.getUserId());
        PeachblossomVo vo = PeachblossomVo.init(userInfo);
        System.out.println(vo);
        vo.setSoundUrl(peachblossom.getSoundUrl());
        vo.setRemainingTimes(Integer.valueOf(s) - 1);
        peachblossom.setState(1);
        peachblossomApi.update(peachblossom);
        return vo;
    }

    //发送传音
    public void sendPeachblossom(MultipartFile soundFile) {
        try {
            String key = Constants.PEACH_RECIVE_KEY + UserHolder.getUserId();
            String s = redisTemplate.opsForValue().get(key);
            if (s == null) {
                redisTemplate.opsForValue().set(key, 9 + "");
                redisTemplate.opsForValue().append("deleteKey", UserHolder.getUserId() + ",");
            } else if (Integer.valueOf(s) != 0) {
                redisTemplate.opsForValue().set(key, Integer.valueOf(s) - 1 + "");
            } else {
                throw new BusinessException(ErrorResult.countZeroError());
            }
            Peachblossom peachblossom = new Peachblossom();
            //解析文件
            String fileName = soundFile.getOriginalFilename();
            fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
            StorePath storePath = client.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(), fileName, null);
            String fullPath = storePath.getFullPath();
            String url = webServer.getWebServerUrl() + fullPath;
            //用户id
            long userId = UserHolder.getUserId();
            //设置参数
            peachblossom.setUserId(userId);
            peachblossom.setCreated(System.currentTimeMillis());
            peachblossom.setSoundUrl(url);
            //保存
            peachblossomApi.saveSound(peachblossom);
            //mqMessageService.sendLogService(UserHolder.getUserId(),"0301","video",id);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}